package src.controller;

import java.rmi.RemoteException;
import src.domain.User;
import src.resource.CommonMethods;
import src.resource.DialogResource;
import src.resource.ValidateResource;
import src.resource.DialogResource.windowTypeEnum;
import src.service.ClientService;
import src.view.AddMailBox;


public class AddMailBoxController {

	private ClientService system;
	private AddMailBox view;
	
	public AddMailBoxController(ClientService system){
		this.view=AddMailBox.getInstance(this);
		this.system=system;
		
	}
	
	private void cleanForm(){
		view.getTxtName().setText("");
		view.getPasswordFieldPass().setText("");
		view.getPasswordFieldConfirmPass().setText("");
		view.getChkActive().setSelected(false);
		
	}
	
	@SuppressWarnings("deprecation")
	private boolean validateRequiredFields(){
		return ValidateResource.validateRequiredField(view.getTxtName().getText()) && ValidateResource.validateRequiredField(view.getPasswordFieldPass().getText())
		&& ValidateResource.validateRequiredField(view.getPasswordFieldConfirmPass().getText());
	}
	
	@SuppressWarnings("deprecation")
	private boolean validateFieldsLenght(){
		return ValidateResource.validateStringLength(view.getTxtName().getText(), 50) && ValidateResource.validateStringLength(view.getPasswordFieldPass().getText(), 50)
		&& ValidateResource.validateStringLength(view.getPasswordFieldPass().getText(), 50);
	}
	
	@SuppressWarnings("deprecation")
	public void performSaveMailBox() throws RemoteException{
		if(!validateRequiredFields()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must complete the required fields.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(!validateFieldsLenght()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The fields can not contain more than 20 characters.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(!view.getPasswordFieldPass().getText().equals(view.getPasswordFieldConfirmPass().getText())){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The password does not match.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(system.getMailBoxByName(view.getTxtName().getText())!=null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Name in use. Please enter another name.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(view.getjListPostUser().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must select a user.",windowTypeEnum.wtInformation);
			return;
		}
			
		if(view.getListModelAddedPostOffices().isEmpty()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must add at least one post office.",windowTypeEnum.wtInformation);
			return;
		}
		
		system.saveMailBox(view.getTxtName().getText(), view.getPasswordFieldPass().getText(), view.getChkActive().isSelected(),(User)view.getjListPostUser().getSelectedValue(), 
				CommonMethods.toSet(this.view.getListModelAddedPostOffices()));
		DialogResource.showMessage(view.getBtnSave().getLocation(),"Information","MailBox saved sucessfully.",windowTypeEnum.wtConfirmation);
		this.cleanForm();
	}
	
	public void performSearchUsers() throws RemoteException{
		//this.cleanForm();
		this.view.getListModelUsers().removeAllElements();
		view.setListModelUsers(CommonMethods.toListModel(system.getUsers(this.view.getTxtSearch().getText())));
		view.getjListPostUser().setModel(view.getListModelUsers());
		
	}
	
	public void performAdd(){
		if(this.view.getjListAvailablePostOffices().getSelectedValue()==null)
			DialogResource.showMessage(view.getBtnAdd().getLocationOnScreen(),"Information","Must select a item to add.",windowTypeEnum.wtInformation);
		this.view.getListModelAddedPostOffices().addElement(view.getjListAvailablePostOffices().getSelectedValue());
		this.view.getListModelAvailablePostOffices().removeElement(view.getjListAvailablePostOffices().getSelectedValue());
	}
	
	public void performRemove(){
		if(this.view.getjListAddedPostOffices().getSelectedValue()==null)
			DialogResource.showMessage(view.getBtnAdd().getLocationOnScreen(),"Information","Must select a item to remove.",windowTypeEnum.wtInformation);
		this.view.getListModelAvailablePostOffices().addElement(view.getjListAddedPostOffices().getSelectedValue());
		this.view.getListModelAddedPostOffices().removeElement(view.getjListAddedPostOffices().getSelectedValue());
	}
	
	
	
	
}

	
	

